package com.chookss.home.audit.clockIn;

import android.graphics.Color;

import com.chookss.R;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ClockInUtils {
    public static String getClockInType(String type) {
        if (Utils.isNull(type)) {
            return "未打卡";
        } else {
//            1-正常 2-迟到 3-旷工 4-早退 5-补卡6暂时无用7请假
            if ("1".equals(type)) {
                return "正常";
            } else if ("2".equals(type)) {
                return "迟到";
            } else if ("3".equals(type)) {
                return "旷工";
            } else if ("4".equals(type)) {
                return "早退";
            } else if ("5".equals(type)) {
                return "补卡";
            } else if ("7".equals(type)) {
                return "请假";
            }
        }
        return "未打卡";
    }

    public static String getClockInReissueCard(String type) {
        //            1-正常 2-迟到 3-旷工 4-早退 5-补卡6暂时无用7请假
        if (Utils.isNull(type)) {
            return "未打卡";
        } else {
            //0补卡中 已通过
            if ("1".equals(type)) {
                return "补卡";
            } else {
                return "补卡中";
            }
        }
    }

    public static int getClockInBg(String type) {
        //            1-正常 2-迟到 3-旷工 4-早退 5-补卡6暂时无用7请假
        if (Utils.isNull(type)) {
            return R.drawable.shape_e5e5e5_2;
        } else {
            if ("1".equals(type)) {
                return R.drawable.shape_00c993_2;
            } else if ("3".equals(type)) {
                return R.drawable.shape_main_color_2;
            } else {
                return R.drawable.shape_fca817_2;
            }
        }
    }

    public static int getClockInRecordBg(String type) {
        //            1-正常 2-迟到 3-旷工 4-早退 5-补卡6暂时无用7请假
        if (Utils.isNull(type)) {
            return R.drawable.shape_e5e5e5_13;
        } else {
            if ("1".equals(type)) {
                //正常
                return R.drawable.shape_00c993_13_1f;
            } else if ("3".equals(type)) {
                return R.drawable.shape_main_color_13_2f;
            } else {
                return R.drawable.shape_fca817_13_1f;
            }
        }
    }

    public static int getClockInRecordColor(String type) {
        //            1-正常 2-迟到 3-旷工 4-早退 5-补卡6暂时无用7请假
        if (Utils.isNull(type)) {
            return Color.parseColor("#9A9A9A");
        } else {
            if ("1".equals(type)) {
                //正常
                return Color.parseColor("#00C993");
            } else if ("3".equals(type)) {
                return Color.parseColor("#E12416");
            } else {
                return Color.parseColor("#FF9D1D");
            }
        }
    }

    public static String getMinutes(String time) {
        if (Utils.isNull(time)) {
            return "";
        } else {
            if (time.length() >= 16) {
                return time.substring(11, 16);
            } else {
                return time;
            }
        }
    }

    public static String getCurrentTime2(long time) {
// 将时间戳转换成日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        return dateFormat.format(new Date(time));
    }

    public static Long getCurrentTime3(String time) {
        // 将日期格式转换成时间戳
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse.getTime();
    }

    public static Long getCurrentTime4(String time, String middle) {
        // 将日期格式转换成时间戳
        Date parse = null;
        try {
            String day = "", minute = "";
            if (Utils.isNull(time) || Utils.isNull(middle)) {
                return 0L;
            } else {
                if (time.length() > 10) {
                    day = time.substring(0, 10);
                } else {
                    day = Utils.getCurrentTime2().substring(0, 10);
                }
                if (middle.length() >= 5) {
                    minute = middle.substring(0, 5);
                } else {
                    minute = "12:00";
                }
            }
            parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(day + " " + minute + ":00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse.getTime();
    }

    public static String getGroupEnd(String code) {
        if (Utils.isNull(code)) {
            return "";
        } else {
            if (code.length() > 4) {
                return code.substring(code.length() - 4, code.length());
            } else {
                return code;
            }
        }
    }

    public static String getMinute(String time) {
        if (Utils.isNull(time)) {
            return "";
        } else {
            if (time.length() >= 16) {
                return time.substring(11, 16);
            } else {
                return time;
            }
        }
    }

    public static String getClockInError(String waiqinCd, String type) {
        if (Utils.isNull(waiqinCd)) {
            return "打卡成功";
        } else {
            if (waiqinCd.equals("1")) {
                 if ("2".equals(type)) {
                    return "打卡时间超过规定时间，此次打卡属于迟到；\n超出打卡范围，此次属于外勤打卡。";
                } else if ("3".equals(type)) {
                    return "打卡时间超过规定时间，此次打卡属于旷工；\n超出打卡范围，此次属于外勤打卡。";
                } else if ("4".equals(type)) {
                    return "打卡时间早于规定时间，此次打卡属于早退；\n超出打卡范围，此次属于外勤打卡。";
                } else {
                    return "超出打卡范围，此次属于外勤打卡。";
                }
            } else {
//                1-正常 2-迟到 3-旷工 4-早退 5-补卡6请假
                if ("1".equals(type)) {
                    return "打卡成功";
                } else if ("2".equals(type)) {
                    return "打卡时间超过规定时间，此次打卡属于迟到。";
                } else if ("3".equals(type)) {
                    return "打卡时间超过规定时间，此次打卡属于旷工。";
                } else if ("4".equals(type)) {
                    return "打卡时间早于规定时间，此次打卡属于早退。";
                } else if ("5".equals(type)) {
                    return "补卡";
                } else if ("6".equals(type)) {
                    return "请假";
                }
            }

        }
        return "打卡成功";
    }

    /**
     * 判断当前日期是星期几
     *
     * @param pTime 设置的需要判断的时间  //格式如2012-09-08
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */

//  String pTime = "2012-03-12";
    public static String getWeek(String pTime) {
        if (Utils.isNull(pTime)) {
            return "";
        }
        if (pTime.length() != 10) {
            return "";
        }
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        c.setFirstDayOfWeek(Calendar.SUNDAY); // 设置每周的第一天为星期日
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "星期日";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "星期一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "星期二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "星期三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "星期四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "星期五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "星期六";
        }


        return Week;
    }

    public static int getTimeInt(String time) {
        if (Utils.isNull(time)) {
            return 9;
        }
        if (time.startsWith("0")) {
            if (time.length() > 1) {
                return Utils.changeNumber(time.substring(1, 2));
            } else {
                Utils.changeNumber(time);
            }
        } else {
            return Utils.changeNumber(time);
        }
        return 9;
    }

    public static String addTimeZero(int num) {
        String newNum = "";
        if (num < 10) {
            newNum = "0" + num;
        } else {
            newNum = num + "";
        }
        return newNum;
    }

    public static String changeTime(String time) {
        if (Utils.isNull(time)) {
            return time;
        } else {
            return time.replace("-", "/");
        }
    }

    public static String calculateDay(String startTime, String endTime, int currentStart, int endStart) {
        float num = 0;
        if (currentStart == 0) {
            //从上午开始的
            num = num + differentDays(startTime, endTime);
            if (currentStart == endStart) {
                num = (float) (num + 0.5);
            } else {
                num = num + 1;
            }
        } else {
            //从下午开始的
            if (differentDays(startTime, endTime) == 0) {
                //当天只有半天
                num = (float) (0.5);
            } else {
                num = num + differentDays(startTime, endTime);
                if (currentStart == endStart) {
                    num = (float) (num + 0.5);
                } else {
                    num = num + 0;
                }
            }
        }

        return ClearMoreZeroUtil.subZeroAndDot(num + "");
    }

    public static int differentDays(String startTime, String endTime) {
        Date date1 = null, date2 = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date1 = format.parse(startTime);
            date2 = format.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) {
            //同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    //闰年
                    timeDistance += 366;
                } else {
//不是闰年
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else {
            //不同年
            return day2 - day1;
        }
    }
}
